<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreScheduleRecordRequest;
use App\Models\ScheduleRecord;
use App\Services\ScheduleRecordService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ScheduleRecordController extends Controller
{
    protected $ScheduleRecordService;
    public function __construct(ScheduleRecordService $ScheduleRecordService)
    {
        $this->ScheduleRecordService = $ScheduleRecordService;
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        if($Request->type == 'start'){
            $type = [1,2];
        }
        if($Request->type == 'end'){
            $type = [3,4];
        }
        $data = ScheduleRecord::whereIn('type',$type)->where('push_id',Auth::user()->id)->where('schedule_id',$Request->id)->first();
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    /**
     * 存储一个新用户。
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreScheduleRecordRequest $Request)
    {
        $POSTONLY = ScheduleRecord::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['push_id'] = Auth::user()->id;
        $data = $this->ScheduleRecordService->insert($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(StoreScheduleRecordRequest $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = ScheduleRecord::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['id'] = $Request->id;
        $data = $this->ScheduleRecordService->update($input);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }
}
